package tmproject.hlhj.fhp.tmvote.customView

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.widget.ImageView
import android.graphics.Canvas.ALL_SAVE_FLAG
import android.graphics.Canvas.ALL_SAVE_FLAG
import android.graphics.Paint.FILTER_BITMAP_FLAG
import android.graphics.Paint.ANTI_ALIAS_FLAG
import android.graphics.PaintFlagsDrawFilter







/**
 * Created by Never Fear   on 2018\8\6 0006.
Never More....
 */
class RectImageView:ImageView {
    private var mPaint:Paint?=null
    private var mXfermode:Xfermode?=null
//    private lateinit var mRectMask:Bitmap
    @JvmOverloads
    constructor(context: Context,attributeSet: AttributeSet?=null,def:Int=0):super(context,attributeSet,def)


    init {
        mPaint=Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint?.color=Color.RED
        mPaint?.isAntiAlias=true
        mXfermode=PorterDuffXfermode(PorterDuff.Mode.DST_IN)
        createMask()
    }

    private fun createMask() {
//        mRectMask= Bitmap.createBitmap(measuredWidth,measuredHeight,Bitmap.Config.ARGB_8888)

    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {

        val id = canvas?.saveLayer(0f, 0f, canvas.getWidth().toFloat(), canvas.getHeight().toFloat(), null, Canvas.ALL_SAVE_FLAG)
        canvas?.drawFilter = PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
        mPaint?.setXfermode(mXfermode);
        val path = Path()
//        path.moveTo(0f,0f)
        path.moveTo(measuredWidth.toFloat()*13/100,0f)
        path.lineTo(measuredWidth.toFloat(),0f)
        path.lineTo(measuredWidth.toFloat()*87/100,measuredHeight.toFloat())
        path.lineTo(0f,measuredHeight.toFloat())

        canvas?.clipPath(path)
        canvas?.drawColor(Color.WHITE)

//        canvas?.clipRect(Rect(0,0,100,100))
        mPaint?.xfermode = null
        super.onDraw(canvas)

        canvas?.restoreToCount(id!!)
//        canvas?.restore()

    }


}